/**
 *Copyright (c) 2024 watereyes
 * workflow is licensed under Mulan PubL v2.
 * You can use this software according to the terms and conditions of the Mulan
 * PubL v2.
 * You may obtain a copy of Mulan PubL v2 at:
 *          http://license.coscl.org.cn/MulanPubL-2.0
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PubL v2 for more details.
 */
package com.koron.workflow.mobile.controller;

import org.koron.ebs.mybatis.ADOConnection;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.swan.bean.MessageBean;

import com.koron.workflow.api.util.ApiUtils;
import com.koron.workflow.common.exception.WorkflowException;
import com.koron.workflow.common.util.BusinessCode;
import com.koron.workflow.mobile.service.MobileService;

@Controller
@RequestMapping("/mobile")
public class MobileController {
	
	private static Logger log = LoggerFactory.getLogger(MobileController.class);
	
	@Autowired
	private MobileService mobileService;

    @ResponseBody
	@RequestMapping(value = "/historyView.htm", method = RequestMethod.GET)
	public ModelAndView historyView(@RequestParam(required = true) String secret, @RequestParam(required = true) String processInstanceId, @RequestParam(required = true) String setCode, @RequestParam(required = true) String processCode){
		//秘钥验证 已包含时间戳校验
		secret = secret.trim().replace(" ", "+");
		MessageBean<?> message = ApiUtils.secretVerify(secret);
		if(message.getCode() != BusinessCode.SUCCESS.getCode()) {
			throw new WorkflowException(message.getDescription());
		}
		String appId = (String) message.getData();
		//判断当前应用是否有操作改流程模板的权限
		message = ApiUtils.processPermission(appId, processCode, setCode);
		if(message.getCode() != BusinessCode.SUCCESS.getCode()) {
			throw new WorkflowException(message.getDescription());
		}
    	message = ADOConnection.runTask(mobileService, "historyView", MessageBean.class, setCode, processCode, processInstanceId);
		if(message.getCode() == BusinessCode.SUCCESS.getCode()) {
			log.info("获取流程表单页面成功, processInstanceId：{} ", processInstanceId);
		} else {
			throw new WorkflowException(message.getDescription());
		}
		return (ModelAndView) message.getData();
    }
    
    @ResponseBody
   	@RequestMapping(value = "/approvalView.htm", method = RequestMethod.GET)
   	public ModelAndView approvalView(@RequestParam(required = true) String secret, @RequestParam(required = true) String taskId, @RequestParam(required = true) String setCode, @RequestParam(required = true) String processCode, @RequestParam(required = true) String operator){
		//秘钥验证 已包含时间戳校验
		secret = secret.trim().replace(" ", "+");
		MessageBean<?> message = ApiUtils.secretVerify(secret);
		if(message.getCode() != BusinessCode.SUCCESS.getCode()) {
			throw new WorkflowException(message.getDescription());
		}
		String appId = (String) message.getData();
		//判断当前应用是否有操作改流程模板的权限
		message = ApiUtils.processPermission(appId, processCode, setCode);
		if(message.getCode() != BusinessCode.SUCCESS.getCode()) {
			throw new WorkflowException(message.getDescription());
		}
    	message = ADOConnection.runTask(mobileService, "approvalView", MessageBean.class, setCode, processCode, taskId, operator);
		if(message.getCode() == BusinessCode.SUCCESS.getCode()) {
			log.info("获取流程表单页面成功, taskId：{} ", taskId);
		}else {
			throw new WorkflowException(message.getDescription());
		}
		return (ModelAndView) message.getData();
    }
}
